<?php

// per directory naming permission regex filter
//
class fs_base {
	public $scheme;
	public $supported = false;
    public $user;
    public $pass;
	public $host;
	public $port;
	public $type;
	public $host_name;
	public $cred_req = false;

    public $displayPath; 	// the path as shown in an address bar (no password)
    public $fullPath;		// full path with username, password, port, etc included

    public $path;		// the path as set by the user
    public $real_path; // the real path of the path set by the user

    public $files;			// contents of the current path

    function __construct () {
    	$tmp = get_class($this);
		if (strpos($tmp,'fs_') === 0){
			$tmp = substr($tmp,3);
			$this->scheme = $tmp;
		}
		if (method_exists($this,'fs_load'))
			$this->fs_load();
    }
    public function __get($name){
        $fn_name = 'get_' . $name;
        if (method_exists($this, $fn_name)){
            return $this->$fn_name();
        } else {
        	return null;
        }
    }
    public function __set($name, $value){
        $fn_name = 'set_' . $name;
        if (method_exists($this, $fn_name)){
            $this->$fn_name($value);
        } else {
        	$stop = '';
        }
    }
    public function get_files(){
    	return $this->files;
    }
    public function get_scheme(){
    	return $this->files;
    }
    public function get_path(){
    	return $this->path;
    }
    public function get_real_path(){
    	return $this->real_path;
    }
    public function get_host_name(){
    	return $this->host_name;
    }
    public function get_supported(){
    	return $this->supported;
    }
}
class fs_file {
	var $name; 	// Ex. apple.txt, folder_name
	var $isDir;
	var $isFile;
	var $size;	// Ex. 0, 2354623
	var $ext;	// Ex. jpg, txt, css, php
	var $type; // Registered file type for this file's extension (Ex. jpg, jpeg, txt, html)


	var $modified;
	var $path = '';
	var $realPath;
	var $basePath;
    var $displayPath; 	// the path as shown in an address bar (no password)
    var $fullPath;		// full path with username, password, port, etc included

	var $isReadable;
	var $isWritable;
	var $isExecutable;
}
class fs_queue_file {
	var $file; 		// fs_file
	var $tmp_required;
	var $tmp_dir;	// the base temp directory + queue name/random string for transfer if needed.
	var $tmp_file;	// temp file name if needed
	var $target;
	var $transfer_type; // move, copy, delete, sync
	var $sync_type; //
	var $state; 	// complete, waiting, active, cancelled, paused, pending (pending target selection or cancel)
	var $progress = 0; // % complete
	var $retries; 	// max number retries. 0 = none, -1 unlimited (unless cancelled)
	var $retried;	// number of retries used
}
class fs_queue {
	var $name = '';
	var $desc;
	var $tmp_dir;	// the base temp directory + queue name/random string for transfer if needed.
	var $files; 	// array of fs_queue_file source (selected) file(s)/folder(s)
	var $target; 	// destination of copy, move, sync.
					// the target can only be a file if a single file is in the $files array
					// otherwise the target is a single folder (that must already exist, for now...)
	var $transfer_type; // move, copy, delete, sync
	var $sync_type; //
	var $state; 	// complete, waiting, active, cancelled, paused, pending (pending target selection or cancel)
	var $progress = 0; // % complete
	var $retries; 	// max number retries. 0 = none, -1 unlimited (unless cancelled)
	function setTarget($target){
		// enum $files
		// if neither the src or the dest are local, the tmp_dir will be used
	}
}
class fs {
	var $tmp_dir; 	// the base temp directory for transfers that require it.
	var $fs; 		// primary connection
    function __construct () {
		$this->tmp_dir = getTmpDir();
    }
    function __wake () {

    }
    public function __get($name){
        $fn_name = 'get_' . $name;
        if (method_exists($this, $fn_name)){
            return $this->$fn_name();
        } else if (method_exists($this->fs, $fn_name)){
            return $this->fs->$fn_name();
        } else {
        	return null;
        }
    }
    public function __set($name, $value){
        $fn_name = 'set_' . $name;
        if (method_exists($this, $fn_name)){
            $this->$fn_name($value);
        } else {
        	$stop = '';
        }
    }
    function browse($newpath) {
    	$mpath = $newpath; // save the path sent to the function so we can make changes to $newpath
    	$scheme = '';
    	// 1st determine if the path is to the local filesystem
    	// 2nd determine if the path has a valid scheme setup, check for attempted hacking attempts on filesystem.
    	// 3rd check permissions (on path and scheme)
    	// 4th if allowed, run the appropriate filesystem handler
    	// Should use a database lookup to match the path to registered file system handlers
    	// each file system should be registered with a regex pattern that will match the scheme/paths it can handle
    	if (preg_match("#local://#i",$newpath)) {
    		// remove the local file system scheme designator for further checks
    		$newpath = substr($newpath,8);
    	}
    	if (preg_match("#^[a-z]:#i", $newpath)) {
    		// windows local file system (Ex c:\, f:, ...)
    		$scheme = "local";
    	} else if (preg_match("#^/#",$newpath)) {
			// linux local file system (Ex /blah/blah, /etc, ... )
			$scheme = "local";
		} else if (preg_match("#^[a-z]{2,10}://#i",$newpath)) {
			// must be something other than local
	        $purl['scheme']= '';
	        $purl['host'] = '';
	        $purl['path'] = '';
	        $purl['login'] = '';
	        $purl['password'] = '';
	        error_reporting(0);
	        $purl = parse_url($newpath);
	        error_reporting(E_ALL & ~E_NOTICE);
			$scheme = $purl['scheme'];
    	}
    	if ($scheme != '') {
			$fs_class = 'fs_' . $scheme;
			if (isset($this->fs)){
				if ($this->fs->scheme != $scheme) {
					unset($this->fs);
					$this->fs = new $fs_class();
				}
			} else {
				$this->fs = new $fs_class();
			}
			return $this->fs->browse($newpath);
    	} else {
    		return false;
    	}
    }
    function copy($src, $dest){

    }
    function move($src, $dest){

    }
    function sync($src, $dest, $args){

    }
    function del($src){

    }
    function addToQueue ($src, $dest, $method) {

    }
    function __sleep () {

    }
    function __destruct () {

    }
    function get($srcfile, $destfile){

    }
    function put($srcfile, $destfile){

    }
    private function clean($path, $use_slash, $remove_preceding_slash) {
        $path = trim($path);
        if ($use_slash == '') {
            $use_slash = '/';
        }
        if (empty($path)) {
            $path = JPATH_ROOT;
        } else {
            // Remove double slashes and backslahses and convert all slashes and backslashes to DS
            $outpath = ereg_replace("\.[\.]+", "", $path);
            if ($remove_preceding_slash) {
                $outpath = ereg_replace("^[\/]+", "", $outpath);
            }
            $outpath = preg_replace('#[/\\\\]+#', $use_slash, $outpath);
            $outpath = ereg_replace("^[A-Za-z][:\|][\/]?", "", $outpath);
        }
        return $outpath;
    }
   }
function getTmpDir(){
	 return realpath(sys_get_temp_dir());
}
if ( !function_exists('sys_get_temp_dir')) {
	function sys_get_temp_dir() {
      if( $temp=getenv('TMP') )        return $temp;
      if( $temp=getenv('TEMP') )        return $temp;
      if( $temp=getenv('TMPDIR') )    return $temp;
      $temp=tempnam(__FILE__,'');
      if (file_exists($temp)) {
          unlink($temp);
          return dirname($temp);
      }
      return null;
	}
}

?>